<?php

namespace app\command;

use support\Log;
use support\Redis;
use think\facade\Db;
use PhpAmqpLib\Wire\AMQPTable;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use App\service\box\BatchBoxServices;


class BoxOrderSuccess extends Command
{
    protected static $defaultName = 'box:order:success';
    protected static $defaultDescription = 'box order success';

    /**
     * @return void
     */
    protected function configure()
    {
        $this->addArgument('name', InputArgument::OPTIONAL, 'Name description');
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
//        $name = $input->getArgument('name');
//        $output->writeln('Hello box:order:success');

        $v_host = env('RABBITMQ_VHOST');
        $exc_name = 'delay_exc_box_order_success';
        $routing_key = 'delay_box_order_success';
        $queue_name = 'delay_queue_box_order_success';
        $connection = new AMQPStreamConnection('localhost', 5672, env('RABBITMQ_USER'), env('RABBITMQ_PWD'), $v_host);
        $channel = $connection->channel();
        $channel->exchange_declare($exc_name, 'x-delayed-message', false, true, false);
        # 后加
        $args = new AMQPTable(['x-delayed-type' => 'direct']);
        $channel->queue_declare($queue_name, false, true, false, false, false, $args);

        $channel->queue_bind($queue_name, $exc_name, $routing_key);

        $callback = function ($msg) use ($output) {
            //$output->writeln($msg->body);

            if (strpos($msg->body, ':') !== false) {
                // 包含 则是  黑洞账号空投盲盒
                $box_id = explode(':', $msg->body)[0]; // 盲盒id
                $buy_count = explode(':', $msg->body)[1]; // 购买数量
                $user_id = explode(':', $msg->body)[2]; // 用户id
                $mobile = explode(':', $msg->body)[3]; // 用户手机号
                $order_pk_id = 0; // 订单号 也是订单表的主键id
                $orderInfo = [];
                $batch_box_id = explode(':', $msg->body)[4]; // 批量空投盲盒表的id
            } else {
                // 否则 则是 盲盒下单购买
                $order_pk_id = $msg->body; // 订单号 也是订单表的主键id

                $orderInfo = Db::name('store_order')->where('id', $order_pk_id)->find();
                $orderUserInfo = Db::name('user')->where('id', $orderInfo['uid'])->find();
                $orderInfo['user'] = $orderUserInfo;
                $batch_box = Db::name('batch_box')->where('order_pk_id', $order_pk_id)->find();

                $box_id = $orderInfo['box_id'];
                $buy_count = $orderInfo['buy_count'];
                $user_id = $orderInfo['uid'];
                $mobile = $orderUserInfo['mobile'];

                $batch_box_id = $batch_box['id'];
            }

            $batchBoxServices = new BatchBoxServices();
            $batchBoxServices->issueBoxExecute((int)$box_id, (int)$buy_count, (int)$user_id, $mobile, (int)$order_pk_id, $orderInfo, (int)$batch_box_id);

            $msg->ack();
        };
        $channel->basic_qos(null, 1, null);
        $channel->basic_consume($queue_name, '', false, false, false, false, $callback);

        while ($channel->is_open()) {
            $channel->wait();
        }

        return self::SUCCESS;
    }

}
